<?php
declare (strict_types=1);

namespace app\model\communal;

use app\constant\LogicConstant;
use app\facade\StringFacade;
use app\model\concern\AmisTrait;
use think\db\Query;
use think\Model;
use think\model\concern\SoftDelete;
use think\model\relation\BelongsTo;
use think\model\relation\HasMany;
use think\model\relation\HasOne;
use think\model\relation\HasOneThrough;
use think\model\relation\MorphOne;

class UserExpertModel extends Model
{
    use AmisTrait;

    /**
     * 数据库配置
     *
     * @var string
     */
    protected $connection = 'communal';

    /**
     * 主键
     *
     * @var string
     */
    protected $pk = 'id';

    /**
     * 表名
     *
     * @var string
     */
    protected $table = '63000_user_expert';

    /**
     * 自动时间戳
     *
     * @var bool
     */
    protected $autoWriteTimestamp = true;

    /**
     * 时间字段设置
     *
     * @var string
     */
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';

    // 设置json类型字段
    protected $json = ['training'];

    // 设置JSON数据返回数组
    protected $jsonAssoc = true;

    /**
     * 软删除
     */
    use SoftDelete;

    protected $deleteTime = 'delete_at';
    protected $defaultSoftDelete = 0;

    /**
     * 展示字段名
     *
     * @var string[]
     */
    public static $column = [
        'id', 'admin_id', 'avatar', 'name', 'gender', 'age', 'mobile', 'is_interpreter', 'title',
        'province', 'province_code', 'city', 'city_code', 'district', 'district_code', 'consult_method',
        // 'fan_number', 'visitor_number',
        'certificate_period', 'helped_number', 'service_time', 'profile', 'edu', 'qualifications', 'expert', 'crowd',
        'service', 'sect', 'language', 'style', 'expert_intro', 'style_intro', 'training',
        'minimum_amount', 'average_amount', 'praise_weight',
        'state', 'created_at'
    ];

    /**
     * 搜索字段名
     *
     * @var string[]
     */
    public static $searchColumn = [
        'id', 'customer_id', 'ids', 'name', 'gender', 'age', 'mobile', 'title', 'is_interpreter',
        'province', 'province_code', 'city', 'city_code', 'district', 'district_code', 'consult_method',
        'state', 'createtime',

        /*
         * app 选项搜索
         */
        'app_code', 'app_service', 'consult_option', 'app_age', 'app_q', 'app_crowd', 'app_others',
    ];

    /**
     * 新增字段
     *
     * @var string[]
     */
    public static $saveColumn = [
        'avatar', 'name', 'gender', 'age', 'mobile', 'is_interpreter', 'title', 'fan_number', 'visitor_number',
        'region', 'consult_method',
        'certificate_period', 'helped_number', 'service_time', 'profile', 'edu', 'qualifications', 'expert', 'crowd',
        'service', 'sect', 'language', 'style', 'expert_intro', 'style_intro', 'training', 'state',
    ];

    /**
     * 更新字段
     *
     * @var string[]
     */
    public static $updateColumn = [
        'id', 'avatar', 'name', 'gender', 'age', 'mobile', 'is_interpreter', 'title', 'fan_number', 'visitor_number',
        'region', 'consult_method',
        'certificate_period', 'helped_number', 'service_time', 'profile', 'edu', 'qualifications', 'expert', 'crowd',
        'service', 'sect', 'language', 'style', 'expert_intro', 'style_intro', 'training', 'state',
    ];

    /**
     * @var string[]
     */
    public static $itemColumn = ['id', 'name', 'state'];

    /**
     * 排序选项
     */
    const ORDER_OPTION_ONE = 1; // 综合排序
    const ORDER_OPTION_TWO = 2; // 低价优先
    const ORDER_OPTION_THREE = 3; // 好评优先
    const ORDER_OPTION_FOUR = 4; // 销量/帮助优先
    const ORDER_OPTION_FIVE = 5; // 时长/服务优先

    /**
     * 排序选项 Options
     */
    public static $orderOptions = [
        ['id' => self::ORDER_OPTION_ONE, 'name' => '综合排序'],
        ['id' => self::ORDER_OPTION_TWO, 'name' => '低价优先'],
        ['id' => self::ORDER_OPTION_THREE, 'name' => '好评优先'],
        ['id' => self::ORDER_OPTION_FOUR, 'name' => '销量优先'],
        ['id' => self::ORDER_OPTION_FIVE, 'name' => '时长优先'],
    ];

    /**
     * 排序规则映射
     *
     * @var string[]
     */
    public static $orderRuleMap = [
        1 => ['orderBy' => 'created_at', 'orderDir' => 'desc'],
        2 => ['orderBy' => 'minimum_amount', 'orderDir' => 'asc'],
        3 => ['orderBy' => 'praise_weight', 'orderDir' => 'desc'],
        4 => ['orderBy' => 'helped_number', 'orderDir' => 'desc'],
        5 => ['orderBy' => 'service_time', 'orderDir' => 'desc'],
    ];

    /**
     * 咨询方式选项
     */
    const CONSULT_OPTION_ONE = 1; // 语音
    const CONSULT_OPTION_TWO = 2; // 视频

    /**
     * 咨询方式选项 Options
     */
    public static $consultOptions = [
        ['id' => self::CONSULT_OPTION_ONE, 'name' => '语音'],
        ['id' => self::CONSULT_OPTION_TWO, 'name' => '视频'],
    ];

    /**
     * 服务均价选项
     */
    const AVERAGE_OPTION_ONE = 1; // 免费
    const AVERAGE_OPTION_TWO = 2; // 付费

    /**
     * 服务均价选项 Options
     */
    public static $averageOptions = [
        ['id' => self::AVERAGE_OPTION_ONE, 'name' => '免费'],
        ['id' => self::AVERAGE_OPTION_TWO, 'name' => '付费'],
    ];

    /**
     * 年龄选项
     */
    const AGE_OPTION_ONE = 1; // 50后
    const AGE_OPTION_TWO = 2; // 60后
    const AGE_OPTION_THREE = 3; // 70后
    const AGE_OPTION_FOUR = 4; // 80后
    const AGE_OPTION_FIVE = 5; // 90后
    const AGE_OPTION_SIX = 6; // 00后

    /**
     * 服务均价选项 Options
     */
    public static $ageOptions = [
        ['id' => self::AGE_OPTION_ONE, 'name' => '50后'],
        ['id' => self::AGE_OPTION_TWO, 'name' => '60后'],
        ['id' => self::AGE_OPTION_THREE, 'name' => '70后'],
        ['id' => self::AGE_OPTION_FOUR, 'name' => '80后'],
        ['id' => self::AGE_OPTION_FIVE, 'name' => '90后'],
        ['id' => self::AGE_OPTION_SIX, 'name' => '00后'],
    ];

    /**
     * 其他筛选选项
     */
    const OTHERS_OPTION_ONE = 1; // 男咨询师
    const OTHERS_OPTION_TWO = 2; // 女咨询师

    /**
     * 服务均价选项 Options
     */
    public static $othersOptions = [
        ['id' => self::OTHERS_OPTION_ONE, 'name' => '男咨询师'],
        ['id' => self::OTHERS_OPTION_TWO, 'name' => '女咨询师'],
    ];

    /**
     * 修改
     *
     * @param $value
     * @return string|string[]
     */
    public function setAvatarAttr($value)
    {
        return StringFacade::replaceDomainShowUrl($value);
    }

    /**
     * avatar 获取
     *
     * @param $value
     * @param $data
     * @return string
     */
    public function getAvatarAttr($value, $data)
    {
        if (empty($value)) {
            // 默认头像
            $value = config('domain.api') . '/static/web/images/default_avatar.png';
        } else {
            $value = StringFacade::splicingDomainShowUrl($value);
        }
        return $value;
    }

    /**
     * app_code 省市区 搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchAppCodeAttr(Query $query, $value)
    {
        if (!empty($value)) {
            $query->whereOr('province_code|city_code|district_code', $value);
        }
    }

    /**
     * app_service 服务类别搜索 待优化
     *
     * @param Query $query
     * @param $value
     */
    public function searchAppServiceAttr(Query $query, $value)
    {
        if (!empty($value)) {
            $value = explode(',', (string)$value);
            $query->where(function (Query $query) use ($value) {
                foreach ($value as $item) {
                    $query->whereOr('service', 'like', '%' . $item . '%');
                }
            });
        }
    }

    /**
     * consult_option 咨询方式搜索 待优化
     *
     * @param Query $query
     * @param $value
     */
    public function searchConsultOptionAttr(Query $query, $value)
    {
        if (!empty($value)) {
            $value = explode(',', (string)$value);
            $query->where(function (Query $query) use ($value) {
                foreach ($value as $item) {
                    $query->whereFindInSet('consult_method', $item);
                }
            });
        }
    }

    /**
     * average_option 服务均价搜索 超无敌高效
     *
     * @param Query $query
     * @param $value
     */
    public function searchAverageOptionAttr(Query $query, $value)
    {
        if (!empty($value)) {
            switch ($value) {
                case 1:
                    // 免费
                    $query->where('average_amount', '<=', 0);
                    break;
                case 2:
                    // 付费
                    $query->where('average_amount', '>', 0);
                    break;
            }
        }
    }

    /**
     * app_age 年龄搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchAppAgeAttr(Query $query, $value)
    {
        if (!empty($value)) {
            $value = explode(',', (string)$value);
            if (isset($value[0])) {
                $query->where('age', '>=', $value[0]);
            }
            if (isset($value[1])) {
                $query->where('age', '<=', $value[1]);
            }
        }
    }

    /**
     * app_q 服务类别搜索 待优化
     *
     * @param Query $query
     * @param $value
     */
    public function searchAppQAttr(Query $query, $value)
    {
        if (!empty($value)) {
            $value = explode(',', (string)$value);
            $query->where(function (Query $query) use ($value) {
                foreach ($value as $item) {
                    $query->whereOr('qualifications', 'like', '%' . $item . '%');
                }
            });
        }
    }

    /**
     * app_crowd 服务类别搜索 待优化
     *
     * @param Query $query
     * @param $value
     */
    public function searchAppCrowdAttr(Query $query, $value)
    {
        if (!empty($value)) {
            $value = explode(',', (string)$value);
            $query->where(function (Query $query) use ($value) {
                foreach ($value as $item) {
                    $query->whereOr('crowd', 'like', '%' . $item . '%');
                }
            });
        }
    }

    /**
     * app_others
     *
     * @param Query $query
     * @param $value
     */
    public function searchAppOthersAttr(Query $query, $value)
    {
        if (!empty($value)) {
            $value = explode(',', (string)$value);
            $query->where(function (Query $query) use ($value) {
                foreach ($value as $item) {
                    switch ($item) {
                        case 1:
                            $query->whereOr('gender', '=', 1);
                            break;
                        case 2:
                            $query->whereOr('gender', '=', 2);
                            break;
                    }
                }
            });
        }
    }


    /**
     * ids 搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchIdsAttr(Query $query, $value)
    {
        if (!empty($value)) {
            $query->whereIn('id', $value);
        }
    }

    /**
     * name 搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchNameAttr(Query $query, $value)
    {
        if (!is_null($value) && strlen((string)$value) > 0) {
            $query->where('name', 'like', '%' . $value . '%');
        }
    }

    /**
     * mobile 搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchMobileAttr(Query $query, $value)
    {
        if (!is_null($value) && strlen((string)$value) > 0) {
            $query->where('mobile', 'like', '%' . $value . '%');
        }
    }

    /**
     * title 搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchTitleAttr(Query $query, $value)
    {
        if (!is_null($value) && strlen((string)$value) > 0) {
            $query->where('title', 'like', '%' . $value . '%');
        }
    }

    /**
     * state 搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchStateAttr(Query $query, $value)
    {
        if (!is_null($value) && strlen((string)$value) > 0) {
            $query->where('state', $value);
        }
    }

    /**
     * 省->区域代码搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchProvinceCodeAttr($query, $value)
    {
        if (!empty($value)) {
            $query->whereIn('province_code', $value);
        }
    }

    /**
     * 城市->区域代码搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchCityCodeAttr($query, $value)
    {
        if (!empty($value)) {
            $query->whereIn('city_code', $value);
        }
    }

    /**
     * 区->区域代码搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchCodeAttr($query, $value)
    {
        if (!empty($value)) {
            $query->whereIn('district_code', $value);
        }
    }

    /**
     * 授权密码
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->password;
    }

    /**
     * 验证字段名
     *
     * @return string
     */
    public function getAuthIdentifierName()
    {
        return 'id';
    }

    /**
     * auth field
     *
     * @return array
     */
    public function getAuthVisibleFields()
    {
        return ['id', 'name', 'mobile', 'state'];
    }

    /**
     * token generate data
     *
     * @return array
     */
    public function getAuthTokenGenerateData()
    {
        $unique = $this->getAuthIdentifierName();
        return [
            $unique => $this->$unique,
        ];
    }

    /**
     * 启用禁用状态
     *
     * @return bool
     */
    public function validateState()
    {
        if (LogicConstant::STATE_ONE == $this->state) {
            return true;
        }
        return false;
    }

    /**
     * 关联附表
     *
     * @return HasOne
     */
    public function token()
    {
        return $this->hasOne(ExpertTokenModel::class, 'expert_id', 'id')
            ->bind([
                'token' => 'token'
            ]);
    }
}
